<template>
  <div class="app-container">
    <div class="filterDiv">
      <label class="label">企业代码</label>
      <el-input v-model="queryParams.orgCode" placeholder="企业代码" clearable size="small" />
      <label class="label">用户账号</label>
      <el-input v-model="queryParams.userAccount" placeholder="用户账号" clearable size="small" />
      <label class="label ml10">用户名</label>
      <el-input v-model="queryParams.userName" placeholder="用户名" clearable size="small" />
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
    </div>
    <div class="contentDiv">
      <el-table v-loading="loading" :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%;">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="会话编号" align="center" prop="token" show-overflow-tooltip />
        <el-table-column label="企业代码" align="center" prop="orgCode" show-overflow-tooltip />
        <el-table-column label="用户账号" align="center" prop="userAccount" show-overflow-tooltip />
        <el-table-column label="用户名" align="center" prop="userName" show-overflow-tooltip />
        <el-table-column label="主机" align="center" prop="ipaddr" show-overflow-tooltip />
        <el-table-column label="登录地点" align="center" prop="loginLocation" show-overflow-tooltip />
        <el-table-column label="浏览器" align="center" prop="browser" />
        <el-table-column label="登录类型" prop="loginType" width="84" align="center">
          <template slot-scope="{row}">
            <span>{{ row.loginType == '*' ? '全部' : row.loginType == '0' ? 'App' : row.loginType == '1' ? '浏览器' : row.loginType == '2' ? '平台' : row.loginType == '9' ? '第三方接口' : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作系统" align="center" prop="os" />
        <el-table-column label="登录时间" align="center" prop="loginTime" width="180" />
        <el-table-column label="最后访问时间" align="center" prop="lastAccessTime" width="180" />
        <el-table-column label="操作" align="center" width="140">
          <template slot-scope="{row}">
            <el-button size="mini" type="text" @click="showOffline(row)">强退</el-button>
            <el-button size="mini" type="text" @click="offSocket(row.userId, row.token)">清除会话</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
    </div>
    <el-dialog :close-on-click-modal="false" title="选择登录类型" :visible.sync="dialogVisible" width="360px">
      <el-form class="radioDiv" label-width="80px">
        <el-form-item label="登录类型">
          <el-radio-group v-model="loginType" style="padding-top:10px">
            <el-radio label="*">全部</el-radio>
            <el-radio label="0">App</el-radio>
            <el-radio label="1">浏览器</el-radio>
            <el-radio label="2">平台</el-radio>
            <el-radio label="9">第三方接口</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="default" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="offLine()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list } from '@/api/monitor/online'
import { killUser, killToken } from '@/api/user'
export default {
  name: 'Online',
  data() {
    return {
      loading: true,
      total: 0,
      list: [],
      userId: '',
      loginType: '',
      pageNum: 1,
      pageSize: 10,
      dialogVisible: null,
      queryParams: {
        userAccount: undefined,
        userName: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      list(this.queryParams).then(res => {
        this.list = res.data.data || []
        this.loading = false
      })
    },
    handleQuery() {
      this.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.handleQuery()
    },
    showOffline(row) {
      this.userId = row.userId
      this.loginType = row.loginType
      this.dialogVisible = true
    },
    offSocket(userId, token) {
      this.$confirm(
        '确定清除会话吗？', '警告',
        {
          confirmButtonText: '确定',
          closeOnClickModal: false,
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        killToken({ userId, token }).then((res) => {
          if (res.data.errorCode === '0') {
            this.$message.success(res.data.msg)
          }
        })
      })
    },
    offLine() {
      const obj = {
        userId: this.userId,
        loginType: this.loginType
      }
      killUser(obj).then((res) => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style scoped>
.radioDiv .el-radio{
  display: block;
  margin-bottom: 10px;
}
</style>
